package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.net.http.HttpRequestHeader;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoZhengJiang;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 安心分期-龙岩市企诺API全量15 镇江
 *
 * @author 李源聪
 */
@ApiPlan(productId = "300", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5LongYanQiNuoZhengJiang implements ApiOrgPhoneMd5ConvertHandler {
    private static final String JX_APPID = "bddbb34c41fa54da";
    private static final String JX_APPKEY = "0719e50be230d07a0f01cf6f2ed4ed4f";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        HttpRequestHeader httpRequestHeader = HttpRequestHeader.create("jx-appid", JX_APPID);
        httpRequestHeader.add("jx-appkey", JX_APPKEY);
        apiDataExchangeParameter.setHttpRequestHeader(httpRequestHeader);
        Map<Object, Object> newMap = new LinkedHashMap<>();
        newMap.put("shouji", apiDataExchangeParameter.getPhoneMd5());
        return newMap;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("status"), 99)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        HttpRequestHeader httpRequestHeader = HttpRequestHeader.create("jx-appid", JX_APPID);
        httpRequestHeader.add("jx-appkey", JX_APPKEY);
        apiApplyParameter.setHttpRequestHeader(httpRequestHeader);
        // 客户姓名
        OrgApplyLongYanQiNuoZhengJiang data = new OrgApplyLongYanQiNuoZhengJiang();
        data.setId(apiApplyParameter.getApplyId());
        data.setName(apiApplyParameter.getUserName());
        data.setShouji(apiApplyParameter.getPhone());
        // 是 否 有 车 (“ 有 ” 或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getCarInfo())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getCarInfo(), "1", "0")
                    .when(apiApplyParameter.getCarInfo(), "2", "1")
                    .when(apiApplyParameter.getCarInfo(), "3", "1")
                    .when(apiApplyParameter.getCarInfo(), "4", "1")
                    .end("0");
            data.setCheliang(value);
        }
        // 年龄(如：39)
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        data.setAge(apiFieldConverter.getAgeInt());
        // 房(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", "0")
                    .when(apiApplyParameter.getRealEstateInfo(), "2", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "3", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "4", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "5", "1")
                    .when(apiApplyParameter.getRealEstateInfo(), "6", "1")
                    .end("0");
            data.setFangchan(value);
        }
        // 保单(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getInsurancePolicy())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getInsurancePolicy(), "1", "0")
                    .when(apiApplyParameter.getInsurancePolicy(), "2", "1")
                    .when(apiApplyParameter.getInsurancePolicy(), "3", "1")
                    .when(apiApplyParameter.getInsurancePolicy(), "4", "1")
                    .end("0");
            data.setBaodan(value);
        }
        // 性别(“男”或“女”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getSex())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getSex(), "0", "男")
                    .when(apiApplyParameter.getSex(), "1", "女")
                    .end("男");
            data.setSex(value);
        }
        // 需求资金 0无1有
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", "1")
                    .when(apiApplyParameter.getLoanAmount(), "2", "1")
                    .when(apiApplyParameter.getLoanAmount(), "3", "1")
                    .when(apiApplyParameter.getLoanAmount(), "4", "1")
                    .end("1");
            data.setXuqiuzj(value);
        }
        // 社保(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getSocialSecurity())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getSocialSecurity(), "1", "0")
                    .when(apiApplyParameter.getSocialSecurity(), "2", "1")
                    .when(apiApplyParameter.getSocialSecurity(), "3", "1")
                    .when(apiApplyParameter.getSocialSecurity(), "4", "1")
                    .end("0");
            data.setShebao(value);
        }
        // 公积金(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getProvidentFund())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getProvidentFund(), "1", "0")
                    .when(apiApplyParameter.getProvidentFund(), "2", "1")
                    .when(apiApplyParameter.getProvidentFund(), "3", "1")
                    .when(apiApplyParameter.getProvidentFund(), "4", "1")
                    .end("1");
            data.setGongjijin(value);
        }
        //是否有企业(“有”或“无”)
        if (ValidParam.isNotEmpty(apiApplyParameter.getBizLicenseYear())) {
            String value = ChooseEq.create()
                    .when(apiApplyParameter.getBizLicenseYear(), "1", "0")
                    .when(apiApplyParameter.getBizLicenseYear(), "2", "1")
                    .when(apiApplyParameter.getBizLicenseYear(), "3", "1")
                    .when(apiApplyParameter.getBizLicenseYear(), "4", "1")
                    .when(apiApplyParameter.getBizLicenseYear(), "5", "1")
                    .end("1");
            data.setGongzi(value);
        }
        ArrayList<OrgApplyLongYanQiNuoZhengJiang> list = new ArrayList<>();
        list.add(data);
        return JsonUtils.toJson(list);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("status"), 99)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
